<?php
namespace app\jiyun\service\base;


use app\common\model\admin\AccountModel;
use app\common\model\admin\ShopStoreModel;
use app\common\model\base\CountryModel;
use app\common\model\jiyun\OrderModel as JyOrderModel;
use app\common\model\jiyun\warehouse\WarehouseAbroadModel;
use app\common\model\jiyun\warehouse\WarehouseModel;
use app\common\model\shop\PlatformModel;

class CommonService
{

    public static $instance=[];

    public static function instance()
    {
        //如果不存在实例，则返回实例
        if (empty(self::$instance)) {

            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 获得仓库列表
     *
     * @return array
     */
    public function getWarehouse ($account_id)
    {
        return WarehouseModel::mk()->where(function ($query) use ($account_id){

            if ($account_id) {
                $query->where('account_id', $account_id);
            }
        })->column('warehouse_c_name', 'id');
        /*if () {

        }*/
        /*WarehouseModel::mk()->where(function ($query) use ($account_id) {
            if ($account_id) {
                ShopStoreModel::mk()->where('relation_account_id', $account_id)->column('')
            }
        })->column('warehouse_c_name', 'id');*/
    }


    /**
     * 获得商品平台
     *
     * @return array
     */
    public function getSystemPlatform ()
    {
        return PlatformModel::mk()->where('1=1')->column('title', 'id');
    }

    /**
     * 获得公司
     *
     * @return array
     */
    public function getAccount()
    {
        //ShopStoreModel::mk()->where('relation_account_id', $account_id)->column('')
        return AccountModel::mk()->where('1=1')->column('title', 'id');
    }


    /**
     * 获得集运订单里面 订单所属的公司
     *
     * @return array
     */
    public function getShopOrderAccount($account_id)
    {
        $shop_account_ids = JyOrderModel::mk()->where(function ($query) use ($account_id){
            if ($account_id) {
                $query->where('account_id', $account_id);
            }
        })->group('shop_account_id')->column('shop_account_id');


        return AccountModel::mk()->whereIn('id', $shop_account_ids)->column('title', 'id');
    }

    /**
     * 获得目的国
     *
     * @param int $key 键字段
     * @param string $field 展示字段
     * @return array
     */
    public function getEndCountry ($key='id', $field='coun_c_name')
    {
        $country_ids = WarehouseAbroadModel::mk()->where('status', 1)->column('country_id');

        return CountryModel::mk()->whereIn('id', $country_ids)->column($field, $key);
    }
}
?>